package com.have.blog.web.controller;

import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.web.dto.BlogDto;
import com.have.blog.web.service.WebBlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RefreshScope
@RequestMapping("/content")
@Api(value = "文章详情相关接口", tags = {"文章详情相关接口"})
@Slf4j
public class BlogController {

    @Autowired
    private WebBlogService blogService;

    @ApiOperation(value = "通过Uid获取博客内容", notes = "通过Uid获取博客内容")
    @GetMapping("/getBlogByUid")
    public Wrapper getBlogByUid(@ApiParam(name = "uid", value = "博客UID", required = false) @RequestParam(value = "uid", name = "uid", required = true) Integer uid) {
        log.info("根据uid获取文章! uid:{}", uid);
        if (StringUtils.isEmpty(uid)) {
            log.error("参数不合法! uid:{}", uid);
            return Wrapper.illegal();
        }
        BlogDto blog = blogService.selectBlogByUid(uid);
        return Wrapper.success(blog);
    }

    @ApiOperation(value = "根据BlogUid获取相关的博客", notes = "根据BlogUid获取相关的博客")
    @GetMapping("/getSameBlogByBlogUid")
    public Wrapper getSameBlogByBlogUid(@ApiParam(name = "blogUid", value = "博客标签UID", required = true) @RequestParam(name = "uid", required = true) Integer uid) {
        if (StringUtils.isEmpty(uid)) {
            return Wrapper.failed();
        }
        List<BlogDto> blogList = blogService.getSameBlogByBlogUid(uid);
        return Wrapper.success(blogList);
    }

    @ApiOperation(value = "通过Uid获取博客点赞数", notes = "通过Uid获取博客点赞数")
    @GetMapping("/getBlogPraiseCountByUid")
    public Wrapper getBlogPraiseCountByUid(@ApiParam(name = "uid", value = "博客UID", required = false) @RequestParam(name = "uid", required = false) Integer uid) {
        return blogService.getBlogPraiseCountByUid(uid);
    }

//    @BussinessLog(value = "通过Uid给博客点赞", behavior = EBehavior.BLOG_PRAISE)
    @ApiOperation(value = "通过Uid给博客点赞", notes = "通过Uid给博客点赞")
    @PostMapping("/praiseBlogByUid")
    public Wrapper praiseBlogByUid(@ApiParam(name = "uid", value = "博客UID", required = false) @RequestParam(name = "uid", required = false) Integer uid) {
        if (StringUtils.isEmpty(uid)) {
            return Wrapper.illegal();
        }
        return blogService.praiseBlogByUid(uid);
    }

}
